package com.snail.generator.exception;

import com.snail.generator.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@Slf4j
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLException.class)
    public R<?> handler(SQLException e) {
        return R.error(e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public R<?> handler(RuntimeException e) {
        return R.error(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public R<?> handler(Exception e) {
        e.printStackTrace();
        return R.error("系统异常");
    }
}
